package il.ac.mta.javaweb.ex1.logic.grids;

import il.ac.mta.javaweb.ex1.logic.ShipSqaure;
import il.ac.mta.javaweb.ex1.logic.players.PlayerBase;

import java.awt.Point;
import java.util.Hashtable;
import java.util.Set;

public class MarkingGrid extends GridBase
{
	private static final String MARK_SQUARE = "?";
	private static final String HIT_SHIP_SQUARE = "X";
	private static final String BOMBED_AND_MISSED = "*";
	BattleShipsGrid battleShipGrid;
	private PlayerBase player;

	public MarkingGrid(BattleShipsGrid grid, PlayerBase player)
	{
		this.battleShipGrid = grid;
		this.player = player;

		initStringGridSquares();
	}

	public PlayerBase getPlayer()
	{
		return player;
	}

	public boolean isSelectedAt(Point position)
	{
		return stringGridSquares[position.x][position.y] == MARK_SQUARE;
	}

	public void mark(Point position)
	{
		if (!stringGridSquares[position.x][position.y].equals(BOMBED_AND_MISSED)
				&& !stringGridSquares[position.x][position.y].equals(HIT_SHIP_SQUARE))
		{
			stringGridSquares[position.x][position.y] = MARK_SQUARE;
		}
	}

	public void markAsBombed(Point position)
	{
		if (!stringGridSquares[position.x][position.y].equals(HIT_SHIP_SQUARE))
		{
			stringGridSquares[position.x][position.y] = BOMBED_AND_MISSED;
		}
	}

	public void unmark(Point position)
	{
		stringGridSquares[position.x][position.y] = "";
	}

	private void initStringGridSquares()
	{
		for (int i = 0; i < stringGridSquares.length; ++i)
		{
			for (int j = 0; j < stringGridSquares[0].length; ++j)
			{
				stringGridSquares[i][j] = " ";
			}
		}
	}

	@Override
	protected Set<Point> getSetOfPositionsOfShips()
	{
		return battleShipGrid.getPositionsOfShips().keySet();
	}

	@Override
	protected Hashtable<Point, ShipSqaure> getMapOfPositionsOfShips()
	{
		return battleShipGrid.getPositionsOfShips();
	}

}
